package com.denso.store.admin.archive;

import com.denso.store.common.model.Archive;
import com.denso.store.common.model.CglibBeanCopierUtils;
import com.denso.store.common.model.Scheduler;
import com.denso.store.common.model.base.BaseArchive;
import com.xiaoleilu.hutool.date.DateUtil;
import com.xiaoleilu.hutool.util.BeanUtil;

import java.util.Date;
import java.util.List;

/**
 * Created by lijian on 2017/10/24.
 */
public class ArchiveService {

    private static final Archive dao = new Archive();

    public List<Scheduler> findSchedulerListToArchive() {
        String dateStr = DateUtil.format(new Date(), "yyyy-MM-dd");
        List<Scheduler> schedulerList = Scheduler.dao.find("select * from scheduler where planTime < ?", dateStr);
        return schedulerList;
    }

    public int archive() {
        List<Scheduler> schedulerList = findSchedulerListToArchive();
        for(Scheduler scheduler : schedulerList) {
            Archive archive = createArchive(scheduler);
            archive.save();
            scheduler.delete();
        }
        return schedulerList.size();
    }

    private Archive createArchive(Scheduler scheduler) {
        Archive archive = new Archive();
        archive.setId(scheduler.getId());
        archive.setCompany(scheduler.getCompany());
        archive.setVan(scheduler.getVan());
        archive.setPlanTime(scheduler.getPlanTime());
        archive.setArriveTime(scheduler.getArriveTime());
        archive.setEnterTime(scheduler.getEnterTime());
        archive.setLeaveTime(scheduler.getLeaveTime());
        archive.setCostTime(scheduler.getCostTime());
        archive.setPosition(scheduler.getPosition());
        archive.setStatus(scheduler.getStatus());
        archive.setLateFlag(scheduler.getLateFlag());
        archive.setAdmin(scheduler.getAdmin());
        archive.setAdminOpt(scheduler.getAdminOpt());
        archive.setCreateTime(scheduler.getCreateTime());
        archive.setUpdateTime(scheduler.getUpdateTime());

        return archive;
    }
}
